﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using DataBaseConnect.App.UserApp;
using DataBaseConnect.App.RoleApp;
using Utils;
using Entity;
using Entity.Dto;
using DataBaseConnect;
using System.Security.Claims;
using DataBaseConnect.App.MessageApp;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class UserController : BaseController
    {
        #region 构造实例化

        public IUserAppService _userAppService;
        public IRoleAppService _roleAppService;
        public IRoleRepository _roleRepository;
        public IMessageAppService _messageAppService;

        public UserController(IUserAppService userAppService, IRoleAppService roleAppService, IRoleRepository roleRepository, IMessageAppService messageAppService)
        {
            _userAppService = userAppService;
            _roleAppService = roleAppService;
            _roleRepository = roleRepository;
            _messageAppService = messageAppService;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新建用户
        /// </summary>
        /// <returns></returns>
        public IActionResult AddUser()
        {
            return View();
        }

        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <returns></returns>
        public IActionResult EditUser(int UserId)
        {
            var user = _userAppService.GetUserFirst(UserId);
            return View(user);
        }

        /// <summary>
        /// 角色设定
        /// </summary>
        /// <param name="UserId">用户id</param>
        /// <returns></returns>
        public IActionResult RoleUser(int UserId)
        {
            UserRoleDto userRoleDto = new UserRoleDto();
            var user = _userAppService.GetUserFirst(UserId);
            var role = _roleAppService.GetRoleList("");
            var roleId = _roleAppService.GetUserRole(UserId);
            userRoleDto.UserId = user == null ? 0 : user.UserId;
            userRoleDto.RoleList = role;
            userRoleDto.RoleId = roleId == null ? 0 : roleId.RoleId;
            userRoleDto.UserRoleId = roleId == null ? 0 : roleId.UserRoleId;
            userRoleDto.RoleUserId = Convert.ToInt32(User.FindFirst("RoleUserId").Value);
            return View(userRoleDto);
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取所有用户&&查询
        /// </summary>
        /// <returns></returns>
        public JsonResult GetUserList(string UserName)
        {
            ReceiveStatus<User> receiveStatus = new ReceiveStatus<User>();
            List<User> list = new List<User>();
            try
            {
                //得到所有菜单
                list = _userAppService.GetUserList(UserName);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 新增用户信息
        /// </summary>
        /// <param name="Input">用户信息实体</param>
        /// <returns></returns>
        public JsonResult AddUserMsg(User Input)
        {
            Message message = new Message();
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _userAppService.AddUserMsg(Input);
                message.Content = "新增用户：" + "【" + Input.UserName + "】";
                message.SendTime = DateTime.Now;
                message.SendUserId = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
                _messageAppService.AddMessage(message);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败：" + ex.Message;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="Input">用户信息实体</param>
        /// <returns></returns>
        public JsonResult EditUserMsg(User Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            Message message = new Message();
            receiveStatus = _userAppService.EditUserMsg(Input);
            message.Content = "修改用户" + "【" + Input.UserName + "】" + "的信息";
            message.SendTime = DateTime.Now;
            message.SendUserId = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
            _messageAppService.AddMessage(message);
            return Json(receiveStatus);
        }

        /// <summary>
        /// 根据主键删除用户信息
        /// </summary>
        /// <param name="UserId">主键</param>
        /// <returns></returns>
        public JsonResult DelUserMsg(List<User> UserId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            Message message = new Message();
            foreach (var item in UserId)
            {
                try
                {
                    _roleRepository.DelUserRoleMsg(f => f.UserId == item.UserId);
                    receiveStatus = _userAppService.DelUserMsg(item.UserId);
                    message.Content = "删除id为" + "【" + item.UserId + "】" + "的用户信息";
                    message.SendTime = DateTime.Now;
                    message.SendUserId = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
                    _messageAppService.AddMessage(message);
                }
                catch (Exception e)
                {
                    receiveStatus.code = statuCode.Error;
                    receiveStatus.msg = "删除失败：" + e.Message;
                }
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="Input">用户信息实体</param>
        /// <returns></returns>
        public JsonResult SetRole(UserRole Input)
        {
            return Json(_roleAppService.SetRole(Input));
        }

        #endregion

    }
}
